/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.goa.deligates;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;
import uk.co.jgo2rdf.exception.AccessionNonConformentException;
import uk.co.jgo2rdf.goa.gp2protein.EntityType;
import uk.co.jgo2rdf.rdf.sio.SIOVocab;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: mhindle
 * Date: 16/02/12
 * Time: 18:22
 * To change this template use File | Settings | File Templates.
 */
public class GenericGenomicDBRDFizer implements EntityRDFizer {

    private final String uri;
    private final Resource dbRecord;
    private final Resource dbIdentifier;
    private final Pattern regex;

    /**
     * @param uri        the uri that prefixes ids
     * @param record     the class of the database record
     * @param identifier the class of the database identifier
     * @param regex      regex to apply to accession
     */
    public GenericGenomicDBRDFizer(String uri, Resource record, Resource identifier, Pattern regex) {
        this.uri = uri;
        this.dbRecord = record;
        this.dbIdentifier = identifier;
        this.regex = regex;
    }

    /**
     * @param uri        the uri that prefixes ids
     * @param record     the class of the database record
     * @param identifier the class of the database identifier
     */
    public GenericGenomicDBRDFizer(String uri, Resource record, Resource identifier) {
        this(uri, record, identifier, null);
    }

    @Override
    public Resource rdfize(Model jenaModel, String accession) throws Exception {
        String copy = accession;

        accession = preProcessAccession(accession);

        if (accession.length() == 0) {
            throw new AccessionNonConformentException(copy + "=processed-to=>" + accession);
        }

        String mainUri = String.format(uri, accession);
        if (mainUri.equals(uri)) {
            mainUri = uri + accession;
        }

        Resource record = jenaModel.createResource(mainUri, dbRecord);
        Resource identifier = jenaModel.createResource(mainUri + "-id", dbIdentifier);

        Resource dna = jenaModel.createResource(mainUri + "-dna", SIOVocab.dna);
        dna.addLiteral(RDFS.label, accession + "-dna");

        record.addProperty(SIOVocab.has_attribute, identifier);
        record.addLiteral(RDFS.label, accession);

        identifier.addLiteral(SIOVocab.has_value, accession);
        identifier.addLiteral(RDFS.label, accession + "-id");

        record.addProperty(SIOVocab.is_about, dna);

        return dna;
    }

    @Override
    public EntityType getType(String accession) throws Exception {
        return EntityType.DNA;
    }

    @Override
    public String preProcessAccession(String accession) {
        String trimmed = accession.trim().toUpperCase();
        if (regex != null) {
            Matcher match = regex.matcher(trimmed);
            match.find();
            return match.group(1);
        }
        return trimmed;
    }
}
